using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Core;

namespace Yuanlin.Business
{
    public class AquacultureSection_PostEdit : YuanlinUserControl,
        IReplyConverter<AquacultureSection_PostReply, GetOperation>
    {
        protected ValuedEditor descriptionValuedEditor;
        protected FileUpload pictureAttachment1;
        protected FileUpload pictureAttachment2;
        protected FileUpload pictureAttachment3;
        protected ResourceButton saveButton;
        protected ResourceButton backButton;
        private AquacultureSection_Post post;
        private int categoryType = UrlManager.Instance.GetParameterValue<int>(YuanlinParameterName.CategoryType);
        private int categoryId = UrlManager.Instance.GetParameterValue<int>(YuanlinParameterName.CategoryId);

        public override void GetRequests(List<BaseRequestBinder> requestBinders)
        {
            requestBinders.Add(RequestHelper.BuildGetAquacultureSection_PostRequest(this));
        }
        protected override void OnLoad(EventArgs e)
        {
            saveButton.Click += new EventHandler(SavePost);
            backButton.Click += new EventHandler(BackButton);
        }

        private void BindData()
        {
            if (post != null)
            {
                descriptionValuedEditor.Value = post.Body.Value;
            }
        }
        private void BackButton(object sender, EventArgs e)
        {
            Page.Response.Redirect(SiteUrls.Instance().GetAquacultureSection_PostListUrl(post.ThreadId.Value, categoryType, categoryId));
        }
        private void SavePost(object sender, EventArgs e)
        {
            if (post != null)
            {
                post.Body.Value =  CensorWordManager.CensorWord(descriptionValuedEditor.Value);

                BusinessManager.UpdateAquacultureSection_Post(post);

                SavePictures(post);

                Page.Response.Redirect(SiteUrls.Instance().GetAquacultureSection_PostListUrl(post.ThreadId.Value, categoryType, categoryId));
            }
        }

        private void SavePictures(AquacultureSection_Post post)
        {
            EntityList postPictures = BusinessManager.GetAquacultureSection_PostPictureList(RequestHelper.BuildGetAquacultureSection_PostPictureListRequest(post.EntityId.Value));

            if (SiteUrls.Instance().IsAttachmentValid(pictureAttachment1))
            {
                if (postPictures.Count > 0)
                {
                    UpdatePostPicture(postPictures[0] as AquacultureSection_PostPicture, pictureAttachment1);
                }
                else
                {
                    CreatePostPicture(post, pictureAttachment1);
                }
            }
            if (SiteUrls.Instance().IsAttachmentValid(pictureAttachment2))
            {
                if (postPictures.Count > 1)
                {
                    UpdatePostPicture(postPictures[1] as AquacultureSection_PostPicture, pictureAttachment2);
                }
                else
                {
                    CreatePostPicture(post, pictureAttachment2);
                }
            }
            if (SiteUrls.Instance().IsAttachmentValid(pictureAttachment3))
            {
                if (postPictures.Count > 2)
                {
                    UpdatePostPicture(postPictures[2] as AquacultureSection_PostPicture, pictureAttachment3);
                }
                else
                {
                    CreatePostPicture(post, pictureAttachment3);
                }
            }
        }
        private void UpdatePostPicture(AquacultureSection_PostPicture postPicture, FileUpload pictureAttachment)
        {
            SiteUrls.Instance().DeleteFile(postPicture);
            postPicture.AttachmentFileName.Value = SiteUrls.Instance().GetPostedFileName(pictureAttachment);
            BusinessManager.UpdateAquacultureSection_PostPicture(postPicture);
            SiteUrls.Instance().SaveFile(SiteUrls.Instance().GetFile(postPicture), pictureAttachment);
        }
        private void CreatePostPicture(AquacultureSection_Post post, FileUpload pictureAttachment)
        {
            AquacultureSection_PostPicture postPicture = new AquacultureSection_PostPicture();
            postPicture.AttachmentFileName.Value = SiteUrls.Instance().GetPostedFileName(pictureAttachment);
            postPicture.AquacultureId.Value = post.AquacultureId.Value;
            postPicture.SectionId.Value = post.SectionId.Value;
            postPicture.ThreadId.Value = post.ThreadId.Value;
            postPicture.PostId.Value = post.EntityId.Value;
            postPicture.CategoryType.Value = categoryType;
            postPicture.CategoryId.Value = categoryId;
            BusinessManager.CreateAquacultureSection_PostPicture(postPicture);
            SiteUrls.Instance().SaveFile(SiteUrls.Instance().GetFile(postPicture), pictureAttachment);
        }

        #region IReplyConverter<AquacultureSection_PostReply,GetOperation> Members

        public void ConvertReply(AquacultureSection_PostReply reply, GetOperation operation)
        {
            if (reply != null)
            {
                post = reply.Entity as AquacultureSection_Post;

                if (!Page.IsPostBack)
                {
                    BindData();
                }
            }
        }

        #endregion
    }
}